package com.cljz.peipei.utils.date;

import android.app.Activity;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.cljz.peipei.R;
import com.cljz.peipei.widget.PickerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**日期类
 * Created by CLJZ-fyw on 2017/5/19.
 */

public class DateTimePickDialogUtil implements OnDateChangedListener{
    private DatePicker datePicker;
    private String initDateTime;
    private String dateTime;
    private Activity activity;
    private PickerView pickerView;
    /**
     * 日期时间弹出选择框构造函数
     *
     * @param activity
     *            ：调用的父activity
     * @param initDateTime
     *            初始日期时间值，作为弹出窗口的标题和日期时间初始值
     */
    public DateTimePickDialogUtil(Activity activity, String initDateTime) {
        this.activity = activity;
        this.initDateTime = initDateTime;
    }

    public void init(DatePicker datePicker) {
        Calendar calendar = Calendar.getInstance();
        if (!(null == initDateTime || "".equals(initDateTime))) {
            calendar = this.getCalendarByInintData(initDateTime);
        } else {
            initDateTime = calendar.get(Calendar.YEAR) + "年"
                    + calendar.get(Calendar.MONTH) + "月"
                    + calendar.get(Calendar.DAY_OF_MONTH) + "日 ";
        }

        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);

    }
/**
 * 弹出日期时间选择框方法
 *
 * @param inputDate
 *            :为需要设置的日期时间文本编辑框
 * @return
 */
public PickerView dateTimePickView(final EditText inputDate){
    LinearLayout dateTimeLayout = (LinearLayout) activity
            .getLayoutInflater().inflate(R.layout.activity_datatime, null);
    datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
    init(datePicker);
    pickerView=new PickerView(activity);
    pickerView.setData((List<String>) dateTimeLayout);
    pickerView.setIsLoop(false);
    pickerView.setCanScroll(true);
    pickerView.setSelected("1997年01月01日");
    inputDate.setText(dateTime);
    onDateChanged(null, 0, 0, 0);
return pickerView;
}


    /**
     * 实现将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒,并赋值给calendar
     *
     * @param initDateTime
     *            初始日期时间值 字符串型
     * @return Calendar
     */
    private Calendar getCalendarByInintData(String initDateTime) {
        Calendar calendar = Calendar.getInstance();

        // 将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒
        String date = spliteString(initDateTime, "日", "index", "front"); // 日期

        String yearStr = spliteString(date, "年", "index", "front"); // 年份
        String monthAndDay = spliteString(date, "年", "index", "back"); // 月日

        String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月
        String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日
        int currentYear = Integer.valueOf(yearStr.trim()).intValue();
        int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1;
        int currentDay = Integer.valueOf(dayStr.trim()).intValue();


        calendar.set(currentYear, currentMonth, currentDay);
        return calendar;
    }

    /**
     * 截取子串
     *
     * @param srcStr
     *            源串
     * @param pattern
     *            匹配模式
     * @param indexOrLast
     * @param frontOrBack
     * @return
     */
    public static String spliteString(String srcStr, String pattern,
                                      String indexOrLast, String frontOrBack) {
        String result = "";
        int loc = -1;
        if (indexOrLast.equalsIgnoreCase("index")) {
            loc = srcStr.indexOf(pattern); // 取得字符串第一次出现的位置
        } else {
            loc = srcStr.lastIndexOf(pattern); // 最后一个匹配串的位置
        }
        if (frontOrBack.equalsIgnoreCase("front")) {
            if (loc != -1)
                result = srcStr.substring(0, loc); // 截取子串
        } else {
            if (loc != -1)
                result = srcStr.substring(loc + 1, srcStr.length()); // 截取子串
        }
        return result;
    }

    @Override
    public void onDateChanged(DatePicker datePicker, int year, int monthOfYear,
                              int dayOfMonth) {
        // 获得日历实例
        Calendar calendar = Calendar.getInstance();

        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        dateTime = sdf.format(calendar.getTime());
        pickerView.setSelected("1997年01月01日");
        pickerView.setData((List<String>) sdf);
        pickerView.setIsLoop(false);
        pickerView.setCanScroll(true);
    }
}
